package channelService

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"go-fox-edge-channel-tcp-server/channelEngine/sockerServer"
)

func Initialized() {
	commConfig.InitializeByFilePath("serverConfig", "serverConfig.json")

	configValue, err := commConfig.GetConfigParam("serverConfig")
	if err != nil {
		commLogger.Error("初始化读取配置参数失败：" + err.Error())
		return
	}

	servers := Map.GetSlice(configValue, "servers", nil)
	for _, v := range servers {
		server := v.(map[string]interface{})

		serverPort := Map.GetInt(server, "serverPort", 0)
		engineParam := Map.GetMap(server, "engine", make(map[string]interface{}))
		engineType := Map.GetString(engineParam, "engineType", "")
		if serverPort == 0 || engineType == "" {
			commLogger.Error("全局配置参数不能为空：engineType, serverPort")
			continue
		}

		if "JavaScript" != engineType {
			commLogger.Error("不支持的engineType：" + engineType)
			continue
		}

		err = sockerServer.StartTcpServer(serverPort, engineParam)
		if err != nil {
			commLogger.Error("创建TCP Server失败：" + err.Error())
		}
	}
}
